package cloudPayAdmin.admin.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;

@Controller
@RequestMapping("/cloudpay/system")
public class SystemController {
	
	@Autowired
	Environment env;
	
	/**
	 * 跳转到错误页
	 * @return
	 */
	@RequestMapping("/toErrorPage")
	public String toError(){
		return "common/error";
	}
	
	/**
	 * 跳转到未授权页
	 * @return
	 */
	@RequestMapping("/toUnAuthorizationPage")
	public String toUnAuthorization() {
		return "common/unAuthorization";
	}
	
	@RequestMapping("/exportExcelData")
	public void  exportExcelData(HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException, IOException {
		String[] headers = StringUtils.splitByWholeSeparator(request.getParameter("header"), "|");
		String[] values = StringUtils.splitByWholeSeparatorPreserveAllTokens(request.getParameter("excelData"), "|");
		
		Workbook wb = new XSSFWorkbook();
		Sheet sheet = wb.createSheet("sheet1");
		Row firstRow = sheet.createRow(0);
		
		//写表头
		for(int i=0;i<headers.length;i++) {
			//Cell cell =	firstRow.createCell(i, Cell.CELL_TYPE_STRING);
			Cell cell =	firstRow.createCell(i, CellType.STRING);
			cell.setCellValue(headers[i]);
		}
		
		//写表格数据
		int rowIndex = 1;
		Row row  = null;
		for(int j=0;j<values.length;j++) {
			//Row row = sheet.createRow(rowIndex);
			if(j % headers.length == 0) {
				row = sheet.createRow(rowIndex);
				rowIndex++;
			}
			
			//Cell cell = row.createCell(j % headers.length, Cell.CELL_TYPE_STRING);
			Cell cell = row.createCell(j % headers.length, CellType.STRING);
			//把html换行符转成window的换行符
			String cellValue = values[j];
			int index = StringUtils.lastIndexOfAny(cellValue, new String[]{"<br/>","<br>","</br>"});
			if(index >= 0) {
				cellValue = StringUtils.substring(cellValue, 0, index);
			}
			cell.setCellValue(StringUtils.replaceEach(cellValue, new String[]{"<br/>","<br>","</br>"}, new String[]{"\r\n","\r\n","\r\n"}));			
		}
		
		String fileName = System.currentTimeMillis() + ".xlsx";
		//解决文件中文乱码   IE浏览器   
        if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0 || request.getHeader("User-Agent").toUpperCase().indexOf("RV:11") > 0){  
        	fileName = URLEncoder.encode(fileName, "UTF-8");       
        }
        else {  //如果非ie浏览器，即谷歌、火狐等
        	fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
        }
        response.reset();   
        response.setContentType("application/octet-stream");    
        response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");    
        
        ServletOutputStream servletOutputStream =  response.getOutputStream();
        
        wb.write(servletOutputStream);
        servletOutputStream.close();
        wb.close();

	}
}
